/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.balisunrise.swing.builder;

import javax.swing.JPanel;
import org.balisunrise.annotation.util.AnnotationsUtilitiesException;
import org.balisunrise.annotation.util.SearchUtilities;
import org.balisunrise.application.Application;
import org.balisunrise.builder.BuilderException;
import org.balisunrise.common.VisualAction;
import org.balisunrise.common.EntityActions;
import org.balisunrise.common.EntityGrid;
import org.balisunrise.common.EntitySelection;
import org.balisunrise.swing.transaction.panel.SelectionPanel;
import org.balisunrise.swing.transaction.panel.layout.SearchLayoutBuilder;

/**
 *
 * @author Glauco
 */
public class EntitySelectionBuilder implements
        org.balisunrise.builder.EntitySelectionBuilder{

    private static final String MESSAGE = "Erro ao construir uma EntitySelection.\n";

    public EntitySelection create(Class entityClass, String alias)
            throws BuilderException {

        if(entityClass == null){
            throw new BuilderException(MESSAGE +
                    "entityClass não pode ser nulo.");
        }

        if(alias == null){
            throw new BuilderException(MESSAGE +
                    "alias não pode ser nulo.");
        }

        try{
            SearchUtilities su = SearchUtilities.create(entityClass, alias);
            return create(su);
        }catch(AnnotationsUtilitiesException aue){
            throw new BuilderException(MESSAGE +
                    aue.getMessage(), aue.getCause());
        }
    }

    public EntitySelection create(SearchUtilities searchUtilities)
            throws BuilderException {

        if(searchUtilities == null){
            throw new BuilderException(MESSAGE +
                    "editorUtilities não pode ser nulo.");
        }

        //cria a busca
        org.balisunrise.swing.transaction.EntitySelection es =
                new org.balisunrise.swing.transaction.EntitySelection();

        // cria o EntityGrid
        EntityGrid<JPanel> eg = Application.get()
                .getVisualEntityManager()
                .getEntityGridBuilder()
                .create(searchUtilities.gridUtilities);

        // Cria o Painel de Ações
        EntityActions<VisualAction,JPanel> ea = Application.get()
                .getVisualEntityManager()
                .getEntityActionBuilder()
                .create(searchUtilities.entityClass,
                        searchUtilities.alias);

        // atribui o pai
        eg.setOwner(es);
        ea.setOwner(es);

        // atribui dados ao Search
        es.setAlias(searchUtilities.alias);
        es.setEntityClass(searchUtilities.entityClass);
        es.setTitle(searchUtilities.title);
        es.setEntityGrid(eg);
        es.setEntityActions(ea);
        es.setVisualRepresentation(new SelectionPanel(
                eg.getVisualRepresentation(),
                ea.getVisualRepresentation(),
                new SearchLayoutBuilder()));

        // nome do painel
        String name = searchUtilities.entityClass.getName() +
                "entitySelecion" + searchUtilities.alias;
        es.getVisualRepresentation().setName(name);
        es.getVisualRepresentation().getAccessibleContext().setAccessibleName(name);
        es.setName(name);
        
        return es;
    }

}
